﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows.Browser;
using Microsoft.LightSwitch.Threading;

namespace LightSwitchApplication
{
    public partial class AddressDetail
    {
        partial void AddressProperty_Loaded(bool succeeded)
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.AddressProperty);
        }

        partial void AddressProperty_Changed()
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.AddressProperty);
        }

        partial void AddressDetail_Saved()
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.AddressProperty);
        }

        partial void AddressDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.AddressProperty != null && this.AddressProperty.CountryRegion == null)
                this.AddressProperty.CountryRegion = DataWorkspace.ApplicationData.CountryRegions.FirstOrDefault();
        }

        partial void AddressDetail_Activated()
        {
            this.DisplayName = this.AddressProperty.Id.ToString() + " (Адрес)";
            //this.FindControl("AddressProperty_FullAddress").SetProperty
        }

        partial void NavigateToMap_Execute()
        {
            {
                string ulrString = @"SiteLocator.htm?id=" + this.AddressProperty.Id.ToString();

                if (this.AddressProperty.Latitude != 0 && this.AddressProperty.Longitude != null && this.AddressProperty.Latitude != 0 && this.AddressProperty.Longitude != 0)
                    ulrString += String.Format(@"&lat={0}&lng={1}", this.AddressProperty.Latitude.ToString(), this.AddressProperty.Longitude.ToString());
                else
                {
                    var addressDefaultgroup = this.DataWorkspace.ApplicationData.SystemParameterGroups.Where(sg => sg.GroupType == "Addresses" && sg.GroupName == "DefaultPosition").Single();
                    var carLat = addressDefaultgroup.SystemParameters.Where(sp => sp.Name == "Latitude").Single().Value;
                    var carLng = addressDefaultgroup.SystemParameters.Where(sp => sp.Name == "Longitude").Single().Value;
                    ulrString += string.Format(@"&lat={0}&lng={1}", carLat, carLng);
                }

                //if (this.AddressProperty.FullAddress != null)
                //    ulrString += @"&add=" + this.AddressProperty.FullAddress; // Utils.SimpleEncoder(this.AddressProperty.FullAddress);
                Dispatchers.Main.Invoke(() =>
                {

                    HtmlPage.Window.Navigate(new Uri(ulrString, UriKind.Relative), "_new");
                });
                //Home.NavigateUrl("http://maps.google.com/maps?q=" + this.AddressProperty.Latitude.ToString() + "," + this.AddressProperty.Longitude.ToString() + "&hl=ru");
            }
        }

        partial void ShowAuditHistory_Execute()
        {
            this.Application.ShowAuditLogHistory("Адрес", this.AddressProperty.Id);
        }

    }
}